<?php


namespace app\common\model\setting;

use app\common\model\user\User;
use tmcore\base\BaseModel;
use tmcore\traits\ModelTrait;

/**
 * 区域模型
 */
class Area extends BaseModel
{
    use ModelTrait;

    /**
     * 获取父类名称
     * @param $value
     * @param $data
     * @return int
     */
    public function getParentNameAttr($value, $data)
    {
        return $data['pid']?Area::where(['id' => $data['pid']])->value('name'):'中国';
    }


    /**
     * 获取父类名称
     * @param $value
     * @param $data
     * @return array
     */
    public function getUidsDescAttr($value)
    {
        $arr = explode(',', $value);
        $result = [];

        foreach ($arr as $key => $value) {
            if($value) {
                $row = User::where(['id' => $value])->field('id, account, avatar')->findOrEmpty();
                if(!$row->isEmpty()) array_push($result, ['id' => $row['id'], 'name' => $row['account'], 'avatar' => $row['avatar']]);
            }
        }

        return $result;
    }
}